package com.example.mylibrary

import android.app.Activity

object ActivityManager {

    private val activityList = mutableListOf<Activity>()

    fun addActivity(activity: Activity) {
        activityList.add(activity)
    }

    fun removeActivity(activity: Activity) {
        activityList.remove(activity)
    }

    fun finishAll() {
        if (activityList.isNotEmpty()) {
            for (activity in activityList) {
                if (activity.isFinishing) {
                    activity.finish()
                }
            }
            activityList.clear()
        }
    }

    fun finishActivity(activity: Activity) {
        if (activityList.isNotEmpty() && activityList.contains(activity)) {
            activityList.remove(activity)
            activity.finish()
        }
    }
}